import { UserConfig, ConfigEnv, loadEnv, defineConfig } from 'vite';
import uni from '@dcloudio/vite-plugin-uni';
import ViteAutoImport from 'unplugin-auto-import/vite';
import ViteAutoComponents from 'unplugin-vue-components/vite';
import Unocss from 'unocss/vite';
import path from 'path';
import { uniuseAutoImports } from '@uni-helper/uni-use';

/**
 * Vite 配置
 * @see https://vitejs.dev/config/
 */
export default defineConfig(({ mode, command }: ConfigEnv): UserConfig => {
  const env = loadEnv(mode, process.cwd());
  return {
    server: {
      host: '0.0.0.0',
      open: true,
      proxy: {
        [env.VITE_APP_BASE_API]: {
          target: 'http://127.0.0.1:8088',
          // target: 'https://jcf-autumn.online/prod-api/',

          changeOrigin: true,
          ws: true,
          rewrite: (path) => path.replace(new RegExp('^' + env.VITE_APP_BASE_API), '')
        }
      }
    },

    build: {
      // 在开发阶段启用源码映射，方便调试
      sourcemap: process.env.NODE_ENV === 'development',
      hotModuleReplacement: true // 开启热更新
    },
    plugins: [
      /**
       * Vite Auto Import 插件，自动导入模块，减少手动导入的工作
       * @see https://github.com/antfu/unplugin-auto-import
       */
      ViteAutoImport({
        imports: ['vue', 'pinia', 'uni-app', uniuseAutoImports()],
        dirs: ['src/utils', 'src/utils/plugins'],
        dts: 'src/types/auto-imports.d.ts'
      }),

      /**
       * Vite Auto Components 插件，自动导入 Vue 组件，减少手动导入和注册的工作
       * @see https://github.com/antfu/unplugin-vue-components
       */
      ViteAutoComponents({
        dirs: ['src/components'],
        extensions: ['vue', 'nvue'],
        dts: 'src/types/auto-components.d.ts'
      }),

      /**
       * unocss
       * @see https://github.com/antfu/unocss
       * see unocss.config.ts for config
       */
      Unocss(),

      /**
       * uni 插件，用于支持 uni-app 项目
       * @see https://github.com/dcloudio/vite-plugin-uni
       */
      uni()
    ]
  };
});
